/**
 * 角色管理的单例
 */
var Role = {
    id: "roleTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Role.initColumn = function () {
    var columns = [
        // {field: 'selectItem', radio: true},
        {field: 'selectItem',   checkbox: true,  visible: true  },
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '名称', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '上级角色', field: 'pName', align: 'center', valign: 'middle', sortable: true},
        {title: '所在部门', field: 'deptName', align: 'center', valign: 'middle', sortable: true},
        {title: '别名', field: 'tips', align: 'center', valign: 'middle', sortable: true},
        {
            title: '操作',
            field: 'operate',
            align: 'center',
            valign: 'middle',
            width:250,
            formatter: operateFormatter //自定义方法，添加操作按钮
        }];
    return columns;
};


/**
 * 检查是否选中
 */
Role.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Role.seItem = selected[0];
        var attr = [];
        for(var i=0;i<selected.length;i++){
            attr.push(selected[i].id);
        }
        Role.seItemId = attr;
        return true;
    }
};

/**
 * 取消选中编辑或者删除按钮那一行
 */
Role.cancelClick = function (id) {
    var checkBox = $("a[data-id='"+id+"']").closest("tr").find("td").eq(0).find("input");
    if(checkBox.is(':checked')){
        checkBox.prop('checked', false);
    }else{
        checkBox.prop('checked', true);
    }
};


/**
 * 点击添加管理员
 */
Role.openAddRole = function () {
    var index = layer.open({
        type: 2,
        title: '添加角色',
        area: ['800px', '450px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/role/role_add'
    });
    this.layerIndex = index;
};

/**
 * 点击修改按钮时
 */
Role.openChangeRole = function (id) {
    Role.cancelClick(id);
    var index = layer.open({
        type: 2,
        title: '修改角色',
        area: ['800px', '450px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/role/role_edit/'+id
    });
    this.layerIndex = index;
};

/**
 * 删除角色
 */
Role.delRole = function (id) {
    Role.cancelClick(id);
    var operation = function(){
        var ids = new Array();
        var ajax = new $ax(Feng.ctxPath + "/role/remove", function (data) {
            Feng.pass(data,function () {
                Feng.success("删除成功!");
                Role.table.refresh();
            });
        }, function (data) {
            Feng.error("删除失败!" + data.msg + "!");
        });
        ids.push(id);
        ajax.set("ids", ids);
        ajax.start();
    };

    Feng.confirm("是否删除角色?",operation);
};

/**
 * 删除角色
 */
Role.delRoleMore = function () {
    if (this.check()) {
        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/role/remove", function () {
                Feng.success("删除成功!");
                Role.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.msg + "!");
            });
            ajax.set("ids", Role.seItemId);
            ajax.start();
        };

        Feng.confirm("是否删除角色?",operation);
    }
};
/**
 * 权限配置
 */
Role.assign = function (roleId) {
    var url = Feng.ctxPath + '/role/role_assign/'+roleId;
    var index = layer.open({
        type: 2,
        title: '权限配置',
        area: ['300px', '450px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: url
    });
    this.layerIndex = index;
};

/**
 * 搜索角色
 */
Role.search = function () {
    var queryData = {};
    queryData['roleName'] = $("#roleName").val();
    Role.table.refresh({query: queryData});
}

$(function () {
    var defaultColunms = Role.initColumn();
    var table = new BSTable(Role.id, "/role/list", defaultColunms);
    table.setPaginationType("client");
    table.init();
    Role.table = table;
});
